	package net.vidmar.tvspored;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlTvHandler extends DefaultHandler {

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean in_displayname = false;
	private boolean in_programme = false;

	private boolean in_title = false;
	private boolean in_titleOriginal = false;
	private boolean in_description = false;
	private boolean in_category = false;
	private boolean in_url = false;

	SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

	private Channel channel = null;
	private Programme programme = null;

	private XmlTvData xmlTvData = new XmlTvData();

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public XmlTvData getParsedData() {
		return this.xmlTvData;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.xmlTvData = new XmlTvData();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {		
		if (localName.equals("channel")) {
			String id = atts.getValue("id");
			channel = new Channel();
			channel.Id = id;
			xmlTvData.addChannel(channel);
		} else if (localName.equals("display-name"))
		{
			in_displayname = true;
		} else if (localName.equals("programme")) {
			
			in_programme = true;
			
			channel = xmlTvData.getChannel(atts.getValue("channel"));
			programme = new Programme();
			channel.addProgramme(programme);
			
			String start = atts.getValue("start");
			String startDate = start.substring(0, 8);
			String startTime = start.substring(8, 10) + ":" + start.substring(10, 12);
			
			try {
				programme.Date = new MyDate(df.parse(startDate));
			} catch (ParseException e1) {
				
			}
			xmlTvData.addDate(programme.Date.Date);
			programme.StartTime = startTime;
			
		} else if (in_programme && localName.equals("title")
				&& atts.getValue("lang").equals("sl")) {
			in_title = true;
		} else if (in_programme && localName.equals("title")
				&& atts.getValue("lang").equals("original")) {
			in_titleOriginal = true;
		} else if (in_programme && localName.equals("desc")) {
			in_description = true;
		} else if (in_programme && localName.equals("category")) {
			in_category = true;
		} else if (in_programme && localName.equals("url")) {
			in_url = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("display-name")) {
			in_displayname = false;
		} else if (localName.equals("programme")) {
			in_programme = false;
		} else if (localName.equals("title")) {				
			in_title = false;
		} else if (localName.equals("desc")) {
			in_description = false;
		} else if (localName.equals("category")) {
			in_category = false;
		} else if (localName.equals("url")) {
			in_url = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String s = new String(ch, start, length);
		if (this.in_displayname) {
			channel.DisplayName = s;
		} else if (this.in_title) {
			programme.Title = s;
		} else if (this.in_titleOriginal) {
			programme.OriginalTitle = s;
		} else if (this.in_description) {
			programme.Description = s;
		} else if (this.in_category) {
			programme.Category = s;
		} else if (this.in_url) {
			programme.WebPage = s;
		}
	}
}
